package cn.xpg.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.xpg.entity.Product;
import cn.xpg.service.ProductService;
import cn.xpg.tools.PageSupport;

@Controller
public class ProductController {

	@Resource(name = "productService")
	private ProductService productService;

	// 分页查询供应商
	@RequestMapping("/getProductList.do")
	public String getproductList(HttpServletRequest request) {
		String pageIndex = request.getParameter("pageIndex");
		String product_name = request.getParameter("product_name");
		int pageSize = 3;
		int currentPageNo = 1;

		if (pageIndex != null) {
			try {
				currentPageNo = Integer.valueOf(pageIndex);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		int totalCount = 0;
		totalCount = productService.getProductCount(product_name);
		System.out.println("查询供应商信息！！！！！！！！！！————>getProductList");
		PageSupport pages = new PageSupport();
		pages.setCurrentPageNo(currentPageNo);
		pages.setPageSize(pageSize);
		pages.setTotalCount(totalCount);

		int totalPageCount = pages.getTotalPageCount();
		if (currentPageNo < 1) {
			currentPageNo = 1;
		} else if (currentPageNo > totalPageCount) {
			currentPageNo = totalPageCount;
		}

		List<Product> productList = null;
		productList = productService.getProductList(product_name, (currentPageNo - 1) * pageSize, pageSize);

		request.setAttribute("productList", productList);
		request.setAttribute("totalPageCount", totalPageCount);
		request.setAttribute("pages", pages);
		request.setAttribute("product_name", product_name);
		return "/jsp/base/product_list";
	}

	// 新增供应商
	@RequestMapping("addProduct.do")
	public String addproduct(@Valid Product product) {
		System.out.println("新增供应商信息！！！！！！！！！！————>addproduct");
		if (productService.addProduct(product) > 0) {
			return "redirect:/getProductList.do";
		} else {
			return "/jsp/base/product_list";
		}
	}

	// 获取供应商Id
	@RequestMapping("getProductId.do")
	public String getproductId(HttpServletRequest request) {
		System.out.println("获取供应商Id！！！！！！！！！！————>getproductId");
		int product_id = Integer.parseInt(request.getParameter("product_id"));
		Product product = productService.getProductId(product_id);
		request.setAttribute("product", product);
		return "/jsp/base/product_update";
	}

	// 修改供应商
	@RequestMapping("updateProduct.do")
	public String updateproduct(@Valid Product product, HttpServletRequest request) {
		System.out.println("修改供应商信息！！！！！！！！！！————>updateproduct");
		if (productService.updateProduct(product) > 0) {
			System.out.println("修改成功！！！！！！");
			return "redirect:/getProductList.do";
		} else {
			System.out.println("修改失败！！！！！！");
			request.setAttribute("product", product);
			return "/jsp/base/product_update";
		}
	}

	// 删除供应商
	@RequestMapping("deleteProduct.do")
	public String deleteproduct(HttpServletRequest request) {
		System.out.println("删除供应商信息！！！！！！！！！！————>deleteProduct");
		int product_id = Integer.valueOf(request.getParameter("product_id"));
		if (productService.deleteProduct(product_id) > 0) {
			System.out.println("删除成功！！！！！！");
		} else {
			System.out.println("删除失败！！！！！！");
		}
		return "redirect:/getProductList.do";
	}
}
